import os
import tarfile
import time


exclude_names = ['.ipynb_checkpoints', 'tmp', 'Untitled', 'nohup', 'log']
def filter_func(tarinfo):
    if max([tarinfo.name.find(exc) for exc in exclude_names]) > -1:
        return None    
    return tarinfo

def backup(backuppath, targetpath):
    targetfile = targetpath + backuppath.split('/')[-2] + '-' + time.strftime("%Y%m%d%H", time.localtime()) + '.tar.bz2'
    with tarfile.open(targetfile, 'w:bz2') as tar:
        basepath = backuppath.split('/')[-2]
        chpath = '/'.join(backuppath.split('/')[:-2])
        os.chdir(chpath)
        for root, dir, files in os.walk(basepath):
            for file in files:
                filename = os.path.join(root, file)
                tar.add(filename, filter=filter_func)
    return (backuppath, targetfile)

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description='Backup Folder')
    parser.add_argument(dest='path',metavar='path', help='backup floder like /home/')
    parser.add_argument(dest='dest',metavar='dest',default='/tmp/', help='backup to /tmp/')
    args = parser.parse_args()
    print(backup(args.path, args.dest))

